package com.jn.fastjson.views;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.jn.fastjson.R;
import com.jn.fastjson.base.BaseActivity;
import com.jn.fastjson.entity.Data;
import com.jn.fastjson.entity.People;
import com.jn.fastjson.entity.School;
import com.jn.fastjson.entity.Student;
import com.jn.fastjson.entity.User;
import com.jn.fastjson.utils.JsonUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private Student mStudent;
    private Student mStudent1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void init() {
        //objToJson();
        //jsonToObj();
        //getJson("news.json");
        //objToJson2();
        jsonToObj2();
        //jsonToObj3();
        //jsonToList();
        //testJsonArray();
        //getValueByKey();
        //testJsonParser();
        //jsonToDate2();
        //jsonToDate();
    }

    private String objToJson() {
        mStudent = new Student();
        mStudent.setStudent_code("25");
        mStudent.setStudent_id(1);
        mStudent.setStudent_name("刘老9");
        mStudent.setUsername("abcdef");
        mStudent.setPassword("123456");
        mStudent.setClass_name("计算机10班");
        String jsonStr = JSON.toJSONString(mStudent);
        Log.d("asdf", "objToJson : " + jsonStr);
        return jsonStr;
    }

    private void jsonToObj() {
        String jsonStr = objToJson();
        mStudent1 = JSON.parseObject(jsonStr, Student.class);
        Log.d("asdf", "jsonToObj = " + mStudent1.toString());
    }

    private void objToJson2() {
        Student student = new Student();
        student.setStudent_code("22");
        student.setStudent_id(1);
        student.setStudent_name("刘老八");
        student.setUsername("abcdef");
        student.setPassword("123456");
        student.setClass_name("计算机10班");

        Student student2 = new Student();
        student2.setStudent_code("22");
        student2.setStudent_id(1);
        student2.setStudent_name("刘老八");
        student2.setUsername("abcdef");
        student2.setPassword("123456");
        student2.setClass_name("计算机10班");

        List<Student> students = new ArrayList<>();
        students.add(student);
        students.add(student2);

        School school = new School();
        school.setName("吉林农业科技学院");
        school.setAddress("吉林市");
        school.setStudents(students);

        String jsonStr = JSON.toJSONString(school);
        Log.d("asdf", "school = " + jsonStr);
    }

    private void jsonToObj2() {
        String jsonStr = getJson("school.json");
        School school = JSON.parseObject(jsonStr, School.class);
        Log.d("asdf", "jsonToObj = " + school.toString());
    }

    private void jsonToObj3() {
        String jsonStr = getJson("student_list.json");
        Data student_list = JSON.parseObject(jsonStr, Data.class);
        Log.d("asdf", "jsonToObj = " + student_list.getData().getGrade2021());
    }

    private void jsonToList() {
        String jsonStr = getJson("peoples.json");
        List<People> list = JSON.parseArray(jsonStr, People.class);

        for (People people : list) {
            Log.d("asdf", "people = " + people.toString());
        }

        JSONArray jsonArray = JSON.parseArray(jsonStr);
        Log.d("asdf", "jsonArray === " + jsonArray.toJSONString());
    }

    private void testJsonArray() {
        JSONArray jsonArray = new JSONArray();
        jsonArray.add("老张");
        jsonArray.add("老李");
        jsonArray.add("老王");
        String jsonStr = jsonArray.toString();
        Log.d("asdf", "json array = " + jsonStr);
    }

    private void getValueByKey() {
        String jsonStr = getJson("news.json");
        String value = JsonUtils.getKeyRecursively(jsonStr, "category");
        Log.d("asdf", "getValueByKey value ==== " + value);
    }

    private void testJsonParser() {
        String jsonStr = getJson("student_list.json");
        //将json字符串转json对象，Feature.OrderedField防止乱序
        JSONObject jsonObject = JSON.parseObject(jsonStr, Feature.OrderedField);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray array = data.getJSONArray("grade2021");
        for (Object object : array) {
            JSONObject jo = (JSONObject) object;
            String name = jo.getString("student_name");
            Log.d("asdf", " ==name== " + name);
        }
    }

    private String getJson(String name) {
        String json = JsonUtils.getJsonFromAssets(this, name);
        Log.d("asdf", "getJson = " + json);
        return json;
    }

    private void jsonToDate() {
        String jsonStr = JsonUtils.getJsonFromAssets(this, "user.json");
        User user = JSON.parseObject(jsonStr, User.class);
        Log.d("asdf", "jsonToDate : " + user.getRegisterDate());
    }

    private void jsonToDate2() {
        String jsonStr = "{\n" +
                "  \"user_name\": \"张三\",\n" +
                "  \"age\": 18,\n" +
                "  \"registerDate\": \"" + System.currentTimeMillis() + "\"\n" +
                "}";
        User user = JSON.parseObject(jsonStr, User.class);
        Log.d("asdf", "jsonToDate2 json : " + jsonStr);
        Log.d("asdf", "jsonToDate2 date : " + user.getRegisterDate());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatDate  = sdf.format(user.getRegisterDate());
        Log.d("asdf", "jsonToDate2 formatDate : " + formatDate);
    }
}